package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * ClassName: PaymentController
 * Package: com.atguigu.springcloud.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-28
 * Time: 21:38
 */

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    //现在是8001服务也要配置集群 就涉及到 80过来访问的时候 到底是访问集群下的
    //哪一个端口 通过value注解来 从配置文件中读取这个端口
    //默负载均衡的方式 就是轮询
    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        //这里的@RequestBody 是为了其他服务来调用的时候传来的JSON格式的字符串
        int result = paymentService.create(payment);
        log.info("*********插入结果: " + result);
        if (result > 0){
            return new CommonResult(200,"插入数据库成功,serverPort" + serverPort,result);
        }else {
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable Long id){
        Payment result = paymentService.getPaymentById(id);
        log.info("*********查询结果: " + result + "hello");
        if (result != null){
            return new CommonResult(200,"查询成功,serverPort" + serverPort,result);
        }else {
            return new CommonResult(444,"查询失败,没有对应记录",null);
        }
    }


}
